//
//  SYGuideViewController.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/8/29.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "SYGuideViewController.h"

@interface SYGuideViewController ()<UIScrollViewDelegate>
{
    NSMutableArray *imageNameArr;
}

@property(nonatomic,strong)UIScrollView *scroll;

@end

@implementation SYGuideViewController

- (UIScrollView *)scroll
{
    if (_scroll==nil) {
        
        _scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
        _scroll.delegate=self;
        _scroll.showsVerticalScrollIndicator=NO;
        _scroll.scrollEnabled=NO;
    }
    return _scroll;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self.view addSubview:self.scroll];
    
    self.scroll.contentSize=CGSizeMake(SCREENWIDTH*5, SCREENHEIGH);
    
    imageNameArr = [[NSMutableArray alloc]init];
    
    if (_webImageNameArr) {
        
        for (int i = 0; i < [_webImageNameArr count]; i++) {
            
            NSString * picURL = [NSString stringWithFormat:@"%@%@",IMAGEHOSTURL,[_webImageNameArr objectAtIndex:i]];
            [imageNameArr addObject:picURL];
        }
        
        [self creatUI];
        
    }else {
        
        [imageNameArr setArray:[self getImageNameArrWithType:self.guideType]];
        
        [self creatUI];
    }
    
}

- (void)creatUI {
    
    for (int i=0; i<imageNameArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, SCREENHEIGH)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[imageNameArr objectAtIndex:i]]];
        [self.scroll addSubview:imageView];
        
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, SCREENHEIGH);
        
        //[button setBackgroundImage:[UIImage imageNamed:[imageNameArr objectAtIndex:i]] forState:UIControlStateNormal];
        button.tag=i+100;
        [button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scroll addSubview:button];
        
        if (_webImageNameArr) {
            
        }else {
            UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame=CGRectMake(SCREENWIDTH*i, 0, SCREENWIDTH, SCREENHEIGH);
            
            [button setBackgroundImage:[UIImage imageNamed:[imageNameArr objectAtIndex:i]] forState:UIControlStateNormal];
            button.tag=i+100;
            [button addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.scroll addSubview:button];
        }
        
        
        
        
    }
    
    if (self.isSkip) {
        
        UIButton *skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [skipBtn setFrame:CGRectMake(SCREENWIDTH - 73, SCREENHEIGH - 52, 56, 28)];
        [skipBtn setImage:[UIImage imageNamed:@"guide_pass_btn_"] forState:(UIControlStateNormal)];
        [skipBtn addTarget:self action:@selector(didMiss) forControlEvents:(UIControlEventTouchUpInside)];
        
        [self.view addSubview:skipBtn];
    }
    
}
- (void)nextAction:(UIButton *)sender
{
    
    int n=(int)(sender.tag-100);
    
    if (n+1<imageNameArr.count) {
        
        _scroll.contentOffset=CGPointMake((n+1)*SCREENWIDTH, 0);
        
    }else
    {
        
        if (self.navigationController) {
            
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
                if (self.viewWasHide) {
                    self.viewWasHide();
                }
                
            }];
            
        }else {
            [self didMiss];
        }
        
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//            if (self.navigationController) {
//                
//                [self.navigationController dismissViewControllerAnimated:YES completion:^{
//                    
//                    if (self.viewWasHide) {
//                        self.viewWasHide();
//                    }
//                    
//                }];
//                
//            }else {
//                [self didMiss];
//            }
//            
//        });
    }
 
}

- (void)didMiss {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        if (self.viewWasHide) {
            self.viewWasHide();
        }
        
    }];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSArray *)getImageNameArrWithType:(NSUInteger)guideType {
    
    NSArray *image = [[NSArray alloc]init];
    switch (guideType) {
        case 100://完成信息
            image = @[@"guide-1",@"guide-2",@"guide-3",@"guide-4",@"guide-5"];
            break;
        case 1://请假
            image = @[@"ios_guide_leave_01",@"ios_guide_leave_02",@"ios_guide_leave_03"];
            break;
        case 2://通知
            image = @[@"ios_ guide_notification_01",@"ios_ guide_notification_02",@"ios_ guide_notification_03"];
            break;
            
        default:
            break;
    }
    
    return image;
}

@end
